<template>
    <span v-if="type == 'format'">
        {{ timestampToView(format) }}
    </span>
  <span v-else-if="type == 'year'">
        {{ timestampToView('YYYYZ') }}</span>
  <span v-else-if="type == 'month'">
        {{ timestampToView('YYYY-MMZ') }}</span>
  <span v-else-if="type == 'day'">
        {{ timestampToView('YYYY-MM-DDZ') }}</span>
  <span v-else-if="type == 'week'">
        {{ timestampToView($t('timestamp.week')) }}</span>
  <span v-else-if="type == 'week-day'">
        {{ timestampToView('YYYY-MM-DDZ') }} ~ {{ timestampToView('YYYY-MM-DDZ', timestampByJS + 86400 * 6000) }}</span>
  <span v-else-if="type == 'hour'">
        {{ timestampToView('YYYY-MM-DDTHH:00Z') }}</span>
  <span v-else-if="type == 'minute'">
        {{ timestampToView('YYYY-MM-DDTHH:mmZ') }}</span>
  <span v-else-if="type == 'second'">
        {{ timestampToView('YYYY-MM-DDTHH:mm:ssZ') }}</span>
  <span v-else>
        {{ timestampToView() }}
    </span>
</template>

<script>
import momentTimezone from 'moment-timezone';

export default {
  name: "Timestamp",
  props: {
    isUtc:{
      type:Number,
      default:1
    },
    type: {
      type: String,
      default: 'default'
    },
    timestamp: {
      type: [String, Number],
      default: 0
    },
    format: String,
    // timezone: String,
  },
  data(){
    return{
      timezone: this.$cookie.get('timezoneName'),
      timezone1:this.$store.state.config.config.timezone, //获取用户选择的时区
    }
  },
  computed: {
    timestampByJS: function () {
      // 转换为数字
      let timestamp = Number(this.timestamp);
      // 转换为毫秒
      return timestamp > 10000000000 ? timestamp : timestamp * 1000;
    }
  },
  mounted () {
    momentTimezone.tz.setDefault(this.$cookie.get('timezoneName'));

  },
  methods: {
    // 将时间戳转换时间
    getData(n) {
      let now = new Date(n),
          y = now.getFullYear(),
          m = now.getMonth() + 1,
          d = now.getDate();
      return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
    },
    offset(data1,offset){
      return new Date(data1 + offset);
    },
    // 将时间戳转换为可视时间
    timestampToView: function (format, timestamp) {
      console.log(this.timezone)
      // console.log(this.getData(timestamp || this.timestampByJS)+this.$store.state.config.config.offset)
      // 2024-04-12 09:12:46
      // console.log(momentTimezone.tz( "2012-11-04 01:00:00-05:00","America/New_York").format())
      if (this.timezone) {
        // 源代码，现在吧时区选择更换了
        // return this.$helper.makeTimestampToDatetime(timestamp || this.timestampByJS, format, this.timezone);
        let getTiume= this.$helper.makeTimestampToDatetime(timestamp || this.timestampByJS, format, this.timezone)
        let offset=this.$cookie.get('timezoneCode')
        let showData=getTiume.substring(0,getTiume.length-6)+offset
        console.log(showData)
        return showData
      } else {
        // 源代码，现在吧时区选择更换了
        // return this.$store.getters['config/filterTimestampToDatetime'](timestamp || this.timestampByJS, format);
        // 把时间格式转换后，如果有夏令时会显示异常 这里做字符串替换
        let getTiume= this.$store.getters['config/filterTimestampToDatetime'](timestamp || this.timestampByJS, format)
        let offset=this.$store.state.config.config.offset
        let showData=getTiume.substring(0,getTiume.length-6)+offset
        console.log(showData)
        return showData


      }
    }
  },
}
</script>

<style scoped>

</style>
